<template>
  <transition name="fade-transform" mode="out-in">
    <div v-loading="loading" :class="className" :style="{'width': width, 'height': height}" />
  </transition>
</template>

<script>
import echarts from "echarts";
import { debounce } from "@/utils";
import { exportExcel } from "@/utils/index";
import { getExcel } from "@/api/chart";
import { parseTime } from "@/utils/index";

export default {
  name: "LineChart",
  props: [
    "data",
    "index",
    "name",
    "num",
    "width",
    "height",
    "time",
    "canDown",
    "groupId",
    "type",
  ],
  data() {
    return {
      title: "",
      id: 0,
      className: "chart" + this.index,
      autoResize: true,
      chart: null,
      selTime: [],
      loading: true,
      end: [],
      xTime: [],
      endName: "偏移量",
      down: "",
      url: "",
      markPoint: [],
      fz: true,
      isAdmin: null,
      some: "x",
      listVos: "xvos",
      firstVos:'f_x'
    };
  },
  watch: {
    type: {
      deep: true,
      handler(n) {
        if (n) {
          switch (n) {
            case "x":
              (this.some = "x"), (this.listVos = "xvos"),(this.firstVos='f_x');
              break;
            case "y":
              (this.some = "y"), (this.listVos = "yvos"),(this.firstVos='f_y');
              break;
            case "z":
              (this.some = "z"), (this.listVos = "zvos"),(this.firstVos='f_z');
              break;
          }
          const optionData = this.data[this.listVos];
          const firstVosData = this.data[this.firstVos];
          
          // console.log(666,optionData)
          if (this.num) {
            this.end = [];
            this.xTime = [];
            optionData.forEach((its) => {
              this.end = [
                ...this.end,
                (its[this.some] - firstVosData).toFixed(2),
              ];
              this.xTime = [
                ...this.xTime,
                parseTime(its.ctime, "{y}-{m}-{d} {h}:{i}:{s}"),
              ];
            });
            this.endName = "偏移量";
          } else {
            this.end = [];
            this.xTime = [];
            optionData.forEach((its) => {
              this.end = [...this.end, Number(its[this.some]).toFixed(2)];
              this.xTime = [
                ...this.xTime,
                parseTime(its.ctime, "{y}-{m}-{d} {h}:{i}:{s}"),
              ];
            });
            this.endName = "距离量";
          }
          this.initData(this.name);
          this.setOptions();
        }
      },
    },
    data: {
      deep: true,
      handler(val) {
        this.initData(this.name);
        this.setOptions(val);
      },
    },
    num: {
      deep: true,
      handler(val) {
        const optionData = this.data[this.listVos];
        const firstVosData = this.data[this.firstVos];
        if (val) {
          this.end = [];
          this.xTime = [];
          optionData.forEach((its) => {
            this.end = [
              ...this.end,
              (its[this.some] - firstVosData).toFixed(2),
            ];
            this.xTime = [
              ...this.xTime,
              parseTime(its.ctime, "{y}-{m}-{d} {h}:{i}:{s}"),
            ];
          });
          this.endName = "偏移量";
          // optionData.forEach((its) => {
          //     console.log(333,'its=',its,'this.some',this.some,its[this.some])
          //   this.end = [
          //     ...this.end,
          //     null,
          //     ...its.map((item) =>
          //       (item[this.some] - its[0][this.some]).toFixed(2)
          //     ),
          //     null,
          //   ];
          //   this.xTime = [
          //     ...this.xTime,
          //     parseTime(its.startTime, "{y}-{m}-{d} {h}:{i}:{s}"),
          //     ...its.map((item) => item.ctime),
          //     parseTime(its.endTime, "{y}-{m}-{d} {h}:{i}:{s}"),
          //   ];
          // });
          // this.end.shift();
          // this.end.pop();
          // this.xTime.shift();
          // this.xTime.pop();
          // this.endName = "偏移量";
        } else {
          this.end = [];
          this.xTime = [];
          optionData.forEach((its) => {
            this.end = [...this.end, Number(its[this.some]).toFixed(2)];
            // this.end = [
            //   ...this.end,
            //   null,
            //   ...its.map((item) => item[this.some].toFixed(2)),
            //   null,
            // ];
            this.xTime = [
              ...this.xTime,
              parseTime(its.ctime, "{y}-{m}-{d} {h}:{i}:{s}"),
            ];
            // this.xTime = [
            //   ...this.xTime,
            //   parseTime(its.startTime, "{y}-{m}-{d} {h}:{i}:{s}"),
            //   ...its.map((item) => item.ctime),
            //   parseTime(its.endTime, "{y}-{m}-{d} {h}:{i}:{s}"),
            // ];
          });
          // this.end.shift();
          // this.end.pop();
          // this.xTime.shift();
          // this.xTime.pop();
          this.endName = "距离量";
        }
        this.initData(this.name);
        this.setOptions();
      },
    },
  },
  mounted() {
    this.isAdmin = localStorage.getItem("isAdmin");
    this.initData(this.name);
    this.initChart();
  },
  methods: {
    /* 初始处理上个页面的数据*/
    initData(data) {
      this.title = `(单位：mm)`;
      this.down = !this.canDown;
      // this.data 就是一个大的集合分段的
      this.typeData();
    },
    typeData() {
      const optionData = this.data[this.listVos];
      const firstVosData = this.data[this.firstVos];
      console.log(666,this.data,this.firstVos)
      // console.log(666,optionData)
      if (this.num) {
        this.end = [];
        this.xTime = [];
        optionData.forEach((its) => {
          this.end = [
            ...this.end,
            (its[this.some] - firstVosData).toFixed(2),
          ];
          this.xTime = [
            ...this.xTime,
            parseTime(its.ctime, "{y}-{m}-{d} {h}:{i}:{s}"),
          ];
        });
        this.endName = "偏移量";
      } else {
        this.end = [];
        this.xTime = [];
        optionData.forEach((its) => {
          this.end = [...this.end, Number(its[this.some]).toFixed(2)];
          this.xTime = [
            ...this.xTime,
            parseTime(its.ctime, "{y}-{m}-{d} {h}:{i}:{s}"),
          ];
          // this.end = [
          //   ...this.end,
          //   null,
          //   ...its.map((item) => item[this.some].toFixed(2)),
          //   null,
          // ];
          // this.xTime = [
          //   ...this.xTime,
          //   parseTime(its.startTime, "{y}-{m}-{d} {h}:{i}:{s}"),
          //   ...its.map((item) => item.ctime),
          //   parseTime(its.endTime, "{y}-{m}-{d} {h}:{i}:{s}"),
          // ];
        });
        // this.end.shift();
        // this.end.pop();
        // this.xTime.shift();
        // this.xTime.pop();
        this.endName = "距离量";
        // if (optionData.length > 1) {
        //   optionData.forEach((item) => {
        //     console.log(456,item)
        //     this.markPoint = [
        //       ...this.markPoint,
        //       {
        //         name: "维护结束",
        //         coord: [
        //           String(parseTime(item.startTime, "{y}-{m}-{d} {h}:{i}:{s}")),
        //           item[0][this.some].toFixed(2),
        //         ],
        //         value: "维护结束",
        //       },
        //       {
        //         name: "维护开始",
        //         coord: [
        //           String(parseTime(item.endTime, "{y}-{m}-{d} {h}:{i}:{s}")),
        //           item[item.length - 1][this.some].toFixed(2),
        //         ],
        //         value: "维护开始",
        //       },
        //     ];
        //   });
        //   this.markPoint.shift();
        //   this.markPoint.pop();
        // }
      }
    },
    setOptions() {
      this.chart.setOption({
        title: {
          text: this.title,
          textStyle: {
            fontSize: 12,
            color: "#008ACD",
          },
        },
        toolbox: {
          feature: {
            mark: { show: true },
            dataView: { show: true, readOnly: false },
            // myTool1: {
            //   show: this.down && this.isAdmin === "true",
            //   title: "数据下载",
            //   icon:
            //     "path://M30.9,53.2C16.8,53.2,5.3,41.7,5.3,27.6S16.8,2,30.9,2C45,2,56.4,13.5,56.4,27.6S45,53.2,30.9,53.2z M30.9,3.5C17.6,3.5,6.8,14.4,6.8,27.6c0,13.3,10.8,24.1,24.101,24.1C44.2,51.7,55,40.9,55,27.6C54.9,14.4,44.1,3.5,30.9,3.5z M36.9,35.8c0,0.601-0.4,1-0.9,1h-1.3c-0.5,0-0.9-0.399-0.9-1V19.5c0-0.6,0.4-1,0.9-1H36c0.5,0,0.9,0.4,0.9,1V35.8z M27.8,35.8 c0,0.601-0.4,1-0.9,1h-1.3c-0.5,0-0.9-0.399-0.9-1V19.5c0-0.6,0.4-1,0.9-1H27c0.5,0,0.9,0.4,0.9,1L27.8,35.8L27.8,35.8z",
            //   onclick: () => {
            //     this.aaa();
            //   },
            // },
            myTool2: {
              title: "反转",
              icon:
                "path://M432.45,595.444c0,2.177-4.661,6.82-11.305,6.82c-6.475,0-11.306-4.567-11.306-6.82s4.852-6.812,11.306-6.812C427.841,588.632,432.452,593.191,432.45,595.444L432.45,595.444z M421.155,589.876c-3.009,0-5.448,2.495-5.448,5.572s2.439,5.572,5.448,5.572c3.01,0,5.449-2.495,5.449-5.572C426.604,592.371,424.165,589.876,421.155,589.876L421.155,589.876z M421.146,591.891c-1.916,0-3.47,1.589-3.47,3.549c0,1.959,1.554,3.548,3.47,3.548s3.469-1.589,3.469-3.548C424.614,593.479,423.062,591.891,421.146,591.891L421.146,591.891zM421.146,591.891",
              onclick: () => {
                this.fz = !this.fz;
                this.initChart();
              },
            },
            // restore: { show: true },
          },
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            animation: true,
            label: {
              backgroundColor: "#505765",
            },
          },
          position: function (pt) {
            return [pt[0], "5%"];
          },
        },
        grid: {
          top: "50px",
          left: "60px",
          right: "50px",
          bottom: "30px",
        },
        calculable: true,
        dataZoom: [
          {
            type: "inside",
            zoomLock: false,
          },
          {
            startValue: "2019-03-31 22:32:36",
            endValue: "2019-04-01 03:32:36",
            handleIcon:
              "M10.7,11.9v-1.3H9.3v1.3c-4.9,0.3-8.8,4.4-8.8,9.4c0,5,3.9,9.1,8.8,9.4v1.3h1.3v-1.3c4.9-0.3,8.8-4.4,8.8-9.4C19.5,16.3,15.6,12.2,10.7,11.9z M13.3,24.4H6.7V23h6.6V24.4z M13.3,19.6H6.7v-1.4h6.6V19.6z",
            handleSize: "80%",
            handleStyle: {
              color: "#fff",
              shadowBlur: 3,
              shadowColor: "rgba(0, 0, 0, 0.6)",
              shadowOffsetX: 2,
              shadowOffsetY: 2,
            },
          },
        ],

        xAxis: [
          {
            type: "category",
            boundaryGap: false,
            // boundaryGap: false,
            data: this.xTime,
          },
        ],
        yAxis: [
          {
            axisLabel: {
              margin: 2,
              formatter: function (value) {
                return value;
              },
            },
            inverse: this.fz,
            scale: true,
            type: "value",
          },
        ],
        series: [
          {
            name: this.endName,
            type: "line",
            showSymbol: false,
            hoverAnimation: false,
            data: this.end,
            itemStyle: {
              normal: {
                color: "#3f9cf1",
                lineStyle: {
                  width: 1.5,
                },
              },
            },
            markPoint: {
              symbolSize: 20,
              data: this.markPoint,
            },
          },
        ],
      });
    },
    aaa() {
      const sta = this.time[0].replace(/-/gi, "/");
      const en = this.time[1].replace(/-/gi, "/");
      window.open(
        `${process.env.VUE_APP_BASE_API}admin/debug/data?start=${sta}&end=${en}&groupId=${this.groupId}`,
        "_blank"
      );
      /* exportExcel(res, '测量数据表格')*/
    },
    initChart() {
      this.chart = echarts.init(this.$el);
      this.setOptions(this.chartData);
      this.loading = false;
    },
  },
};
</script>
